function fillNum(s) {
  s -= 0; // 防止s==='00'
  return s < 10 ? '0' + s : s;
}

export const filter = {
  formatAddress(address, start = 8, end = 4) {
    return address.substring(0, start) + '...' + address.substring(address.length - end, address.length);
  },
  toPercent(amount) {
    return amount ? `${new $big(amount).times(100)}%` : '';
  },
  formatDate(date, rule = 'YYYY-MM-DD HH:mm:ss') {
    if (!date) return '';
    return window.moment(new Date(date)).format(rule);
  },
  toFixed(num, dp = 2) {
    const numArr = num.toString().split('.');
    numArr[1] !== undefined && numArr[1].length > dp && (numArr[1] = numArr[1].substring(0, dp));
    return Number(Number(numArr.join('.')).toFixed(dp));
  },
  countDown(timeDifference/* [,时间差]：秒 */) {
    const day = Math.floor(timeDifference / 86400);
    const hour = Math.floor((timeDifference % 86400) / 3600);
    const minute = Math.floor((timeDifference % 86400 % 3600) / 60);
    const second = Math.floor(timeDifference % 86400 % 3600 % 60);
    return {
      day: fillNum(day),
      hour: fillNum(hour),
      minute: fillNum(minute),
      second: fillNum(second),
    };
  },
};
const install = Vue => Object.entries(filter).forEach(([key, func]) => Vue.filter(key, func));

export default {
  filter,
  install,
};
